// 时间格式化 yyyy-mm-dd
const dateFormat = (date) => {
  return `${date.getFullYear()}-${getZero(date.getMonth() + 1)}-${getZero(date.getDate())}`
}
const dateFormatQingfen = (date) => {
  return `${date.getFullYear()}${getZero(date.getMonth() + 1)}${getZero(date.getDate())}`
}
// 时间格式化 yyyy-mm-dd
const dateFormat2 = (date) => {
  return date.split('-').join('')
}

// 获取时间戳
const getTime = (date) => {
  return new Date(date).getTime()
}

// 位数补零
const getZero = (month) => {
  return month < 10 ? `0${month}` : month
}

// 获取本旬本月本年
const getChangeDate = (i = 0) => {
  let endTime = ''
  let startData = ''
  let endData = ''
  const tenDays = [
    {isTenDay: 1, start: '-01', end: '-10'},
    {isTenDay: 2, start: '-11', end: '-20'},
    {isTenDay: 3, start: '-21', end: endTime}
  ]
  const today = getDay()
  let num = 0
  if (today >= 1 && today <= 10) {
    num = 1
    endTime = tenDays.find(item => item.isTenDay === num).end
  } else if (today > 10 && today <= 20) {
    num = 2
    endTime = tenDays.find(item => item.isTenDay === num).end
  } else if (today > 20 && today <= 31) {
    num = 3
    endTime = getDuration(getYearOrMonth() + '-' + getDay())
  }
  if (i === 0) {
    startData = getYearOrMonth() + '-' + (getDay() - 1)
    endData = getYearOrMonth() + '-' + (getDay() - 1)
  } else if (i === 1) {
    startData = getYearOrMonth() + tenDays.find(item => item.isTenDay === num).start
    endData = getYearOrMonth() + '-' + endTime
  } else if (i === 2) {
    startData = getYearOrMonth() + '-01'
    endData = getYearOrMonth() + '-' + getDuration(getYearOrMonth() + '-' + getDay())
  } else {
    startData = getYearOrMonth('year') + '-01' + '-01'
    endData = getYearOrMonth('year') + '-12' + '-31'
  }
  return {
    startData, endData
  }
}

// 获取yyyy or yyyy-mm
const getYearOrMonth = (d = '') => {
  const date = new Date()
  return d === 'year'
    ? `${date.getFullYear()}`
    : `${date.getFullYear()}-${getZero(date.getMonth() + 1)}`
}

// 获取mm or mm-dd
const getMonthOrDay = (d = '') => {
  const date = new Date()
  return d === 'month'
    ? `${getZero(date.getMonth() + 1)}`
    : `${getZero(date.getMonth() + 1)}-${getZero(date.getDate())}`
}

// 获取当月天数
const getDuration = (date) => {
  const dt = new Date(date)
  dt.setDate(1)
  dt.setMonth(dt.getMonth() + 1)
  dt.setDate(0)
  return dt.getDate()
}

// 获取日
const getDay = () => {
  
  const date = new Date()
  return `${date.getDate()}`
}

// 获取前一天
const getYesterday = () => {
  const day = new Date()
  day.setTime(day.getTime() - 24 * 60 * 60 * 1000)
  return dateFormat(day)
}

// 获取今天之前的第 ${n} 天

const getNDateBefore = (n) => {
  const today = new Date().getTime()
  const nDay = today - (n * 3600 * 24 * 1000)
  return dateFormat(new Date(nDay))
}
const getNDateBeforeQingfen = (n) => {
  const today = new Date().getTime()
  const nDay = today - (n * 3600 * 24 * 1000)
  return dateFormatQingfen(new Date(nDay))
}
// 获取今天之后的第 ${n} 天
const getNDateAfter = (n) => {
  const today = new Date().getTime()
  const nDay = today + (n * 3600 * 24 * 1000)
  return dateFormat(new Date(nDay))
}

// 获取某天之前的第${n}天
const getNDateBeforeNDate = (n, m) => {
  const today = new Date(m).getTime()
  const nDay = today - (n * 3600 * 24 * 1000)
  return dateFormat(new Date(nDay))
}
const getNDateBeforeNDateQingfen = (n, m) => {
  const today = new Date(m).getTime()
  const nDay = today - (n * 3600 * 24 * 1000)
  return dateFormatQingfen(new Date(nDay))
}
const formatMoney = (value) => {
  if (value === null || value === undefined || value === '') {
    return '0.00'
  }
  const stringValue = value.toString()
  const parts = stringValue.split('.')
  let integerPart = parts[0]
  const decimalPart = parts.length > 1 ? parts[1] : ''

  // 处理整数部分的千位符
  integerPart = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',')

  // 组合整数和小数部分
  return decimalPart ? `${integerPart}.${decimalPart}` : integerPart
}
export {
  dateFormat,
  dateFormat2,
  dateFormatQingfen,getNDateBeforeQingfen,getNDateBeforeNDateQingfen,
  getTime,
  getYearOrMonth,
  getChangeDate,
  getYesterday,
  getNDateBefore,
  getNDateAfter,
  getDay,
  getDuration,
  getNDateBeforeNDate,
  getMonthOrDay,
  getZero,
  formatMoney
}
